from django.db import models
from django.views import generic



class Book(models.Model):

    """ Книга пользователя """

    title = models.CharField(max_length=100, verbose_name='Книга')
    description = models.TextField(max_length=920, null=True, blank=True, verbose_name='Описание')
    content = models.TextField(verbose_name='Содержание')
    added = models.DateTimeField(auto_now_add=True, verbose_name='Добавлена')
    is_read = models.BooleanField(default=False, verbose_name='Прочитана')
    is_defer = models.BooleanField(default=False, verbose_name='Отложена')
    is_favorites = models.BooleanField(default=False, verbose_name='Избранная')
    bookmark = models.PositiveIntegerField(default=0, null=True, blank=True, verbose_name='Закладка')
    author = models.ForeignKey('Author', on_delete=models.CASCADE, verbose_name='Автор')
    image = models.ImageField('Обложка', null=True, blank=True, upload_to='%Y/%m/%d')
    volume = models.PositiveIntegerField(null=True, blank=True, verbose_name='Объем книги')
    group = models.ForeignKey('Group', on_delete=models.CASCADE, verbose_name='Группа')
    slug = models.SlugField(verbose_name='URL')
    file = models.FileField(upload_to='list_of_books', verbose_name='Книга')
    background = models.ImageField(null=True, blank=True, upload_to='%Y/%m/%d', verbose_name='Фоновое изображение')
    last_page = models.PositiveIntegerField('Последняя страница', default=0, null=True, blank=True)

    class Meta:
        verbose_name = 'Книга'
        verbose_name_plural = 'Книги'
        ordering = ['-added']

    def __str__(self):
        return self.title


class Quote(models.Model):

    """ Цитата из какой-то книги """

    content = models.TextField(verbose_name='Цитата')
    book = models.ForeignKey(Book, on_delete=models.CASCADE, verbose_name='Книга')

    class Meta:
        verbose_name = 'Цитата'
        verbose_name_plural = 'Цитаты'

    def __str__(self):
        return self.content


class Author(models.Model):

    """ Автор какой-то книги """

    name = models.CharField(max_length=100, verbose_name='Имя')
    description = models.TextField(null=True, blank=True, verbose_name='Описание')
    image = models.ImageField(null=True, blank=True, upload_to='%Y/%m/%d/')
    slug = models.SlugField(verbose_name='URL')

    class Meta:
        verbose_name = 'Автор'
        verbose_name_plural = 'Авторы'

    def __str__(self):
        return self.name


class Group(models.Model):

    """ Группа книг """

    title = models.CharField(max_length=100, verbose_name='Название')
    description = models.TextField(null=True, blank=True, verbose_name='Описание')
    image = models.ImageField(null=True, blank=True, upload_to='%Y/%m/%d/')
    slug = models.SlugField(verbose_name='URL')

    class Meta:
        verbose_name = 'Группа'
        verbose_name_plural = 'Группы'

    def __str__(self):
        return self.title
